﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;


namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class ReMachineTestEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }

        protected override string DWZRef
        {
            get { return "ReMachineTestList"; }
        }

        protected int TestId
        {
            get {
                return GetRequestValue<int>("TestId", 0);
            }
        }

        private ReMachineTestInfo _obj = null;
        protected ReMachineTestInfo obj
        {
            get {
                if (_obj == null)
                {
                    _obj = ReMachineTestController.GetList(TestId);
                }
                return _obj;
            }
        }
        private List<ReMachineTestDetailInfo> _detail = null;
        protected List<ReMachineTestDetailInfo> Detail
        {
            get
            {
                if (_detail == null)
                {
                    _detail = ReMachineTestController.GetDetailByTestID(TestId);
                }
                return _detail;
            }
        }

        //private List<ReMachineTestTempleteInfo> _allTempList;
        //protected List<ReMachineTestTempleteInfo> allTempList
        //{
        //    get
        //    {
        //        if (_allTempList == null)
        //            _allTempList = ReMachineTestController.GetTempleteByParTmpId(0);
        //        return _allTempList;
        //    }
        //}
        protected string GetReMachineTestDetailHtml()
        {
            string html = "";// ReMachineTestController.DC.Get<string>("GetReMachineTestDetailHtml");
           // if (!string.IsNullOrEmpty(html))
           //     return html;
            // List<OMailLineTempleteInfo> allTempList=  OMailLineController.GetAllOMailLineTemplete();
            List<ReMachineTestTempleteInfo> allGWList = ReMachineTestController.GetTempleteByParTmpId(0);// allTempList.Distinct(new OMailLineTempleteDistinctComparer()).OrderBy(e => e.MLTID).ToList();

            string gwItmHtmlResultItm = "";
            string hiddenTemID = "<input type='hidden' name='TemID' value='{0}' />";
            string inputCheckResult = "<input type='text' name='CheckResult' value='{0}' />";

            int sametrint = 0;
            foreach (ReMachineTestTempleteInfo _tmp in allGWList)
            {
                List<ReMachineTestTempleteInfo> _tmpList = ReMachineTestController.GetTempleteByParTmpId(_tmp.TemID);
                int rowspan = _tmpList.Count();
                string gwItmHtml = "<tr ><td >{0}</td><td >{1}{2}</td></tr>";
                string gwItmHtml2 = "<tr><td rowspan={0}>{1}</td><td >{2}</td><td >{3}{4}</td></tr>";
                int tagI = 0;

                foreach (ReMachineTestTempleteInfo _tmpItem in _tmpList)
                {

                    ReMachineTestDetailInfo reObj= Detail.Single<ReMachineTestDetailInfo>(e => e.TemID == _tmpItem.TemID);
                    if (tagI == 0)
                    {
                        gwItmHtmlResultItm += string.Format(gwItmHtml2, rowspan, _tmp.TemContent, _tmpItem.TemContent, string.Format(inputCheckResult, reObj.CheckResult), string.Format(hiddenTemID, _tmpItem.TemID));
                    }
                    else
                    {
                        gwItmHtmlResultItm += string.Format(gwItmHtml, _tmpItem.TemContent, string.Format(inputCheckResult, reObj.CheckResult), string.Format(hiddenTemID, _tmpItem.TemID));
                    }

                    tagI++;
                }

            }



            html = gwItmHtmlResultItm;

           // OMailLineController.DC.Add("GetReMachineTestDetailHtml", html);
            return html;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu2();

            }
        }
        protected void addmenu2()
        {

            ReMachineTestInfo objMail = Request.SerializableObject<ReMachineTestInfo>();


            string[] TemIDS = Request.GetRequestValue<string>("TemID").Split(new char[] { ',' });
            string[] CheckResults = Request.GetRequestValue<string>("CheckResult").Split(new char[] { ',' });


            List<ReMachineTestDetailInfo> listDetail = new List<ReMachineTestDetailInfo>();
            for (int i = 0; i < TemIDS.Count(); i++)
            {
                string TemID = TemIDS[i];
                if (string.IsNullOrEmpty(TemID))
                    continue;
                ReMachineTestDetailInfo itmObj = new ReMachineTestDetailInfo();


                itmObj.TemID = Convert.ToInt32(TemIDS[i]);
                itmObj.CheckResult = CheckResults[i];


                listDetail.Add(itmObj);
            }

            // this.AddLog(Permission.新增, null, objMail);
            if (ReMachineTestController.Update(objMail, listDetail))
                SuccessJson("修改成功", this.DWZRef);
            else
                ErrorJson("修改失败", this.DWZRef);
        }
    }
}